Imports CapaDeDatos
Public Class nRemitoServicios
    Private TblRemitoServicio As CapaDeDatos.dRemitoServicio = New CapaDeDatos.dRemitoServicio
    Private OrdenServicio As CapaDeNegocios.nOrdenServicio = New CapaDeNegocios.nOrdenServicio
    Private SolicitudServicio As CapaDeNegocios.nSolicitudServicio = New CapaDeNegocios.nSolicitudServicio
    Private TblListaPrecio As CapaDeNegocios.nListaDePrecio = New CapaDeNegocios.nListaDePrecio
    Public Function existe(ByVal numero As Long) As Boolean
        Return TblRemitoServicio.Existe(numero)
    End Function
    Public Function Insertar(ByVal CodigoRemito As Long, ByVal numero As Long, ByVal Fecha As Date, ByVal CodigoOrdenServicio As Long, ByVal ValorCotizado As Double, ByVal detalle As String) As Boolean
        Return TblRemitoServicio.Insertar(numero, Fecha, CodigoOrdenServicio, ValorCotizado, detalle)
    End Function
    Public Function Listar() As DataSet
        Return TblRemitoServicio.Listar()
    End Function
    Public Function Modificar(ByVal CodigoRemito As Long, ByVal numero As Long, ByVal Fecha As Date, ByVal CodigoOrdenServicio As Long, ByVal ValorCotizado As Double, ByVal detalle As String, ByVal CodigoFactura As Long) As Boolean
        Return TblRemitoServicio.Modificar(CodigoRemito, numero, Fecha, CodigoOrdenServicio, ValorCotizado, detalle, CodigoFactura)
    End Function
    Public Function Eliminar(ByVal CodigoRemito As Long) As Boolean
        Return TblRemitoServicio.Eliminar(CodigoRemito)
    End Function
    Public Function Buscar(ByVal CodigoRemito As Long, ByVal numero As Long, ByVal Fecha As Date, ByVal CodigoOrdenServicio As Long, ByVal ValorCotizado As Double, ByVal detalle As String, ByVal CodigoFactura As Long, ByVal estado As String) As DataSet
        Return TblRemitoServicio.Buscar(CodigoRemito, numero, Fecha, CodigoOrdenServicio, ValorCotizado, detalle, CodigoFactura, estado)
    End Function

    Public Function BuscarFiltrado(ByVal CodigoRemito As String, ByVal numero As String, ByVal Fecha As String, ByVal CodigoOrdenServicio As String, ByVal ValorCotizado As String, ByVal detalle As String, ByVal CodigoCliente As String, ByVal IdFactura As String, ByVal estado As String) As DataSet

        Dim dts As DataSet
        Dim DtsSalida As DataSet = New DataSet()
        dts = TblRemitoServicio.BuscarFiltrado(CodigoRemito, numero, Fecha, CodigoOrdenServicio, ValorCotizado, detalle, IdFactura, estado)
        DtsSalida.Tables.Add("Tabla0")
        DtsSalida.Tables(0).Columns.Add("CodigoRemito")
        DtsSalida.Tables(0).Columns.Add("Numero")
        DtsSalida.Tables(0).Columns.Add("Fecha")
        DtsSalida.Tables(0).Columns.Add("CodigoOrdenServicio")
        DtsSalida.Tables(0).Columns.Add("ValorCotizado")
        DtsSalida.Tables(0).Columns.Add("Detalle")
        DtsSalida.Tables(0).Columns.Add("IdFactura")
        DtsSalida.Tables(0).Columns.Add("Estado")
        DtsSalida.Tables(0).Columns.Add("IdCliente")


        dts.Tables(0).Columns.Add("IdCliente")
        Dim i As Integer
        For i = 0 To dts.Tables(0).Rows.Count - 1
            Try
                dts.Tables(0).Rows(i)("IdCliente") = SolicitudServicio.Buscar(OrdenServicio.BuscarFiltrado(dts.Tables(0).Rows(i)("CodigoOrdenServicio").ToString(), "", "", "", 0).Tables(0).Rows(0)("CodigoSolicitudServicios").ToString(), "", "", "", CodigoCliente, "", "").Tables(0).Rows(0)("CodigoCliente").ToString()

                If dts.Tables(0).Rows(i)("IdCliente") = CodigoCliente Or CodigoCliente = "" Then
                    Dim dr As DataRow
                    dr = DtsSalida.Tables(0).NewRow()
                    dr("CodigoRemito") = dts.Tables(0).Rows(i)("CodigoRemito").ToString()
                    dr("Numero") = dts.Tables(0).Rows(i)("Numero").ToString()
                    dr("Fecha") = dts.Tables(0).Rows(i)("Fecha").ToString()
                    dr("CodigoOrdenServicio") = dts.Tables(0).Rows(i)("CodigoOrdenServicio").ToString()
                    dr("ValorCotizado") = dts.Tables(0).Rows(i)("ValorCotizado").ToString()
                    dr("Detalle") = dts.Tables(0).Rows(i)("Detalle").ToString()
                    dr("IdFactura") = dts.Tables(0).Rows(i)("IdFactura").ToString()
                    dr("Estado") = dts.Tables(0).Rows(i)("Estado").ToString()
                    dr("IdCliente") = dts.Tables(0).Rows(i)("IdCliente").ToString()
                    DtsSalida.Tables(0).Rows.Add(dr)
                End If
            Catch ex As Exception
                dts.Tables(0).Rows(i)("IdCliente") = -1
            End Try
        Next
        Return DtsSalida
    End Function

    Public Function SumaSubtotales(ByVal Subtotal1 As Double, ByVal subtotal2 As Double, ByVal resultado As Double) As Double
        Dim SumA As Double = Subtotal1
        Dim Sumb As Double = subtotal2
        resultado = CDbl(SumA) + CDbl(Sumb)
        Return resultado

    End Function

    Public Function Valorizar(ByVal CantidadKilometros As Double, ByVal CantidadHoras As Double, ByVal CodigoTunidad As Long) As DataSet
        Dim dtsListaPrecio As DataSet
        Dim CodigoLista As Long
        Dim SubKM As Double
        Dim SubHS As Double
        Dim ValorKM As Double
        Dim ValorHs As Double
        Dim Total As Double
        Dim dtsValorizar As DataSet = New DataSet
        Dim dr As DataRow
        dtsValorizar.Tables.Add("tabla")
        dtsValorizar.Tables(0).Columns.Add("SubHs")
        dtsValorizar.Tables(0).Columns.Add("SubKM")
        dtsValorizar.Tables(0).Columns.Add("Total")
        dtsValorizar.Tables(0).Columns.Add("id_ListaPrecio")
        Try
            dtsListaPrecio = TblListaPrecio.Buscar(0, New Date(), 0, 0, "", CodigoTunidad)
            CodigoLista = dtsListaPrecio.Tables(0).Rows(0)("CodigoLista")
            ValorKM = CDbl(dtsListaPrecio.Tables(0).Rows(0)("ValorKM"))
            ValorHs = CDbl(dtsListaPrecio.Tables(0).Rows(0)("ValorHS"))
            SubKM = CantidadKilometros * ValorKM
            SubHS = CantidadHoras * ValorHs
            Total = SubHS + SubKM

            'dtsValorizar.Tables(0).Rows(0)("SubHs") = SubHS.ToString()
            'dtsValorizar.Tables(0).Rows(0)("SubKM") = SubKM.ToString()
            'dtsValorizar.Tables(0).Rows(0)("SubTotal") = Total.ToString()

            dr = dtsValorizar.Tables(0).NewRow()
            dr("SubHs") = SubHS.ToString()
            dr("SubKM") = SubKM.ToString()
            dr("Total") = Total.ToString()
            dr("id_ListaPrecio") = CodigoLista
            dtsValorizar.Tables(0).Rows.Add(dr)
        Catch ex As Exception
        End Try
        Return dtsValorizar
    End Function

    Public Function BuscarUltimoRemito() As Long
        Return TblRemitoServicio.BuscarUltimoRemito()
    End Function



End Class



